﻿using System.Collections.Generic;
using static Utils.CharacterMod;
using System.Linq;
namespace Neutron3529.BlackMarket;
[System.AttributeUsage(System.AttributeTargets.Field, AllowMultiple = false)]
public class Length : System.Attribute {
    public int method;
    public Length(int method) {
        this.method=method;
    }
}
public interface ICData {
    public void From(GameData.Domains.Character.Character ch);
    public void Into(GameData.Domains.Character.Character ch, GameData.Common.DataContext ctx);
    public int size(); // inner size
    public int Size()=>this.size()+4; // wrapper size
    public unsafe byte* Ser(byte* ptr);
    public unsafe byte* Des(byte* ptr);
    public unsafe byte* S(byte* ptr){
        *(int*)ptr=this.VERSION;
        ptr=this.Ser(ptr+4);
        return ptr;
    }
    public unsafe byte* D(byte* ptr){
        if(this.VERSION==*(int*)ptr){
            ptr=this.Des(ptr+4);
        } else {
            throw new System.Exception("Version Mismatch!");
        }
        return ptr;
    }
    public int VERSION {get;}
    public sbyte GetIdeal {get;}
    public GameData.Domains.Character.AvatarSystem.AvatarData GetAvatar{get;}
}
public partial class CharacterData : ICData {
    public int VERSION=>1;
    public sbyte GetIdeal=>(sbyte)(this.ideals>>24);
    public GameData.Domains.Character.AvatarSystem.AvatarData GetAvatar=>this.Avatar;
    public unsafe void From(GameData.Domains.Character.Character ch){
        this.gender=(uint)((((byte)(ch.birthMonth()))<<24) | ( ((byte)(ch.happiness()))<<16) | (ch.haveRightLeg()?1<<13:0)|(ch.haveLeftLeg()?1<<12:0)|(ch.haveRightArm()?1<<11:0)|(ch.haveLeftArm()?1<<10:0)|(ch.transgender() ? 1<<9:0) | (ch.bisexual()?1<<8:0) | (byte)(ch.gender()));
        this.xiangshuInfection=(uint)(((((byte)ch.lifeSkillQualificationGrowthType())) <<24) | ((((byte)ch.combatSkillQualificationGrowthType()))<<16) | ((((byte)(ch.consummateLevel()))) << 8) | ch.xiangshuInfection());
        this.ideals=(uint)((((byte)(ch.idealSect()))<<24) | ((((byte)(ch.lifeSkillTypeInterest()))<<16)) | ((((byte)(ch.combatSkillTypeInterest()))<<8)) | ((byte)(ch.mainAttributeInterest())));
        // working.public short (.+); -> this.\1=ch.\1();
        this.CurrAge=ch.currAge();
        this.ActualAge=ch.actualAge();
        this.BaseMorality=ch.baseMorality();
        this.Health=ch.health();
        this.BaseMaxHealth=ch.baseMaxHealth();
        this.DisorderOfQi=ch.disorderOfQi();
        {
            var x = ch.GetBaseLifeSkillQualifications();
            this.lifeSkillQualifications = new short[16];
            for (int index = 0; index < 16; index++)
                this.lifeSkillQualifications[index] = x.Items[index];
        }
        {
            var x = ch.GetBaseCombatSkillQualifications();
            this.combatSkillQualifications = new short[14];
            for (int index = 0; index < 14; index++)
                this.combatSkillQualifications[index] = x.Items[index];
        }
        {
            var x = ch.GetBaseMainAttributes();
            this.maxMainAttributes = new short[6];
            for (int index = 0; index < 6; index++)
                this.maxMainAttributes[index] = x.Items[index];
        }
        this.Avatar=ch.GetAvatar();
        List<short> featureIds=new(ch.GetFeatureIds());
        featureIds.Remove(197); // TODO : 这里是bypass，删除身怀六甲特性。实际上应该判定有无身怀六甲并上传孩子数据
        this.featureIds=featureIds.ToArray();
        this.potentialFeatureIds=ch.GetPotentialFeatureIds().ToArray();
    }
    public unsafe void Into(GameData.Domains.Character.Character ch, GameData.Common.DataContext ctx){
        ch.birthMonth((sbyte)(this.gender>>24),ctx);
        ch.happiness((sbyte)(this.gender>>16),ctx);
        ch.haveRightLeg((this.gender&(1<<13))>0,ctx);
        ch.haveLeftLeg((this.gender&(1<<12))>0,ctx);
        ch.haveRightArm((this.gender&(1<<11))>0,ctx);
        ch.haveLeftArm((this.gender&(1<<10))>0,ctx);
        ch.transgender((this.gender&(1<<9))>0,ctx);
        ch.bisexual((this.gender&(1<<8))>0,ctx);
        ch.gender((sbyte)this.gender,ctx);
        ch.lifeSkillQualificationGrowthType((sbyte)(this.xiangshuInfection>>24),ctx);
        ch.combatSkillQualificationGrowthType((sbyte)(this.xiangshuInfection>>16),ctx);
        ch.consummateLevel((sbyte)(this.xiangshuInfection>>8),ctx);
        ch.xiangshuInfection((byte)(this.xiangshuInfection),ctx);
        ch.idealSect((sbyte)(this.ideals>>24),ctx);
        ch.lifeSkillTypeInterest((sbyte)(this.ideals>>16),ctx);
        ch.combatSkillTypeInterest((sbyte)(this.ideals>>8),ctx);
        ch.mainAttributeInterest((sbyte)(this.ideals),ctx);
        ch.currAge(this.CurrAge,ctx);
        ch.actualAge(this.ActualAge,ctx);
        ch.baseMorality(this.BaseMorality,ctx);
        ch.health(this.Health,ctx);
        ch.baseMaxHealth(this.BaseMaxHealth,ctx);
        ch.disorderOfQi(this.DisorderOfQi,ctx);
        {
            var x = ch.GetBaseLifeSkillQualifications();
            for (int index = 0; index < 16; index++)
                x.Items[index]=this.lifeSkillQualifications[index];
            ch.SetBaseLifeSkillQualifications(ref x,ctx);
        }
        {
            var x = ch.GetBaseCombatSkillQualifications();
            for (int index = 0; index < 14; index++)
                x.Items[index]=this.combatSkillQualifications[index];
            ch.SetBaseCombatSkillQualifications(ref x,ctx);
        }
        {
            var y = ch.GetCurrMainAttributes();
            var x = ch.GetBaseMainAttributes();
            for (int index = 0; index < 6; index++){
                y.Items[index]=0;
                x.Items[index]=this.maxMainAttributes[index];
            }
            ch.SetCurrMainAttributes(y,ctx);
            ch.SetBaseMainAttributes(x,ctx);
        }
        ch.SetAvatar(this.Avatar,ctx);
        ch.SetFeatureIds(this.featureIds.ToList(),ctx);
        ch.SetPotentialFeatureIds(this.potentialFeatureIds.ToList(),ctx);
    }
    // working public uint gender;// (BirthMonth<<24) | ( Happiness<<16) | (HaveRightLeg?1<<13:0)|(HaveLeftLeg?1<<12:0)|(HaveRightArm?1<<11:0)|(HaveLeftArm?1<<10:0)|(Transgender ? 1<<9:0) | (Bisexual?1<<8:0) | Gender
    // working public uint xiangshuInfection; // (LifeSkillQualificationGrowthType <<24) | (CombatSkillQualificationGrowthType<<16) | (ConsummateLevel << 8) | XiangshuInfection
    // working public uint ideals;// (IdealSect <<24) | (LifeSkillTypeInterest<<16) | (CombatSkillTypeInterest<<8) | MainAttributeInterest
    // working public short CurrAge;
    // working public short ActualAge;
    // working public short BaseMorality;
    // working public short Health;
    // working public short BaseMaxHealth;
    // working public short DisorderOfQi;

    // reorder to align results fit align
    // working public short[] lifeSkillQualifications; // length=16
    // working public short[] combatSkillQualifications; // length=14
    // working public short[] maxMainAttributes; // length=6
    // dyn
    // working public GameData.Domains.Character.AvatarSystem.AvatarData Avatar;
    // working public short[] featureIds;
    // working public short[] potentialFeatureIds;
}
